from pygame import mixer
from Constant import *
import pygame

mixer.init(44100)
pygame.mixer.init()
'''
Sound :

    The object is used for playing effect sound and background music.
'''
class Sound():
    '''
    Constructor : Sound
    '''
    def __init__(self):
        self._001_initialInstanceVariable()
        self.timerPlayMusic.Start(1500)
    '''
    function _001 : initialize the variables used for this object
    '''
    def _001_initialInstanceVariable(self):
        self.listSound = {}
        self.music = {}
        self.isPlayEffect = True
        self.isPlayBackground = True
        self.curPlayMusicName = None
        self.timerPlayMusic = wx.Timer()
        self.timerPlayMusic.Bind(wx.EVT_TIMER, self.SLOT001_checkMusicEnd)
    '''
    function ACT001 : play passing name of background music at loops 
    '''
    def ACT001_playBackgroundMusic(self, name):
        if self.isPlayBackground :
            pygame.mixer.music.stop()
            pygame.mixer.music.load(self.music[ str(name) ])
            pygame.mixer.music.play(1)
            self.curPlayMusicName = str(name)
    '''
    function ACT002 : play passing name of effect sound one time 
    '''
    def ACT002_playSound(self, name):
        if self.isPlayEffect :
            self.listSound[ str(name) ].play()
    '''
    function ACT003 : load effect sound of passing file name, and call the sound to referred name
    '''
    def ACT003_loadSound(self, fileName, name):
        self.listSound[ str(name) ] = mixer.Sound(fileName)
        self.listSound[ str(name) ].set_volume(0.5)
        
    def ACT004_loadBackgroundMusic(self, fileName, name):
        self.music[ str(name) ] = fileName
    '''
    function SET001 : set instance variable - isPlayEffect
    '''
    def SET001_isPlayEffect(self, isPlayEffect):
        self.isPlayEffect = isPlayEffect
    '''
    function SET002 : set instance variable - isPlayBackground
    '''
    def SET002_isPlayBackground(self, isPlayBackground):
        if self.isPlayBackground != isPlayBackground :
            self.isPlayBackground = isPlayBackground
            if not isPlayBackground :
                pygame.mixer.music.stop()
                self.timerPlayMusic.Stop()
            else :
                self.ACT001_playBackgroundMusic(self.curPlayMusicName)
                self.timerPlayMusic.Start(1500)
    '''
    function GET001 : get instance variable - isPlayEffect
    '''
    def GET001_isPlayEffect(self):
        return self.isPlayEffect
    '''
    function GET002 : get instance variable - isPlayBackground
    '''
    def GET002_isPlayBackground(self):
        return self.isPlayBackground
    '''
    function GET003 : get instance variable - curPlayMusicName
    '''
    def GET003_curPlayMusicName(self):
        return self.curPlayMusicName
    
    def SLOT001_checkMusicEnd(self, event):
        if self.isPlayBackground :
            if (self.curPlayMusicName != None) and (not pygame.mixer.music.get_busy()) :
                self.ACT001_playBackgroundMusic(self.curPlayMusicName)